package com.demostic.demostic_test.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.demostic.demostic_test.R;
import com.demostic.demostic_test.ui.EditTipsDialog;
import com.demostic.demostic_test.ui.MyDialog;
import com.demostic.demostic_test.ui.PatNameDialog;
import com.demostic.demostic_test.ui.SelectDialog;

public class DialogUtils {
    public static AlertDialog showDialog(Context context, View contentView) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(contentView);
        AlertDialog dialog = builder.create();
        Window dialogWindow = dialog.getWindow();
        if (dialogWindow != null) {
            dialogWindow.setWindowAnimations(R.style.dialog_anim);
            dialogWindow.setGravity(80);
        }
        dialog.show();
        return dialog;
    }

    public static void hideDialog(AlertDialog dialog) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public static Dialog createLoadingDialog(Context context) {
        View contentView = LayoutInflater.from(context).inflate(R.layout.layout_loading_dialog, (ViewGroup) null);
        Dialog loadingDialog = new Dialog(context, R.style.MyDialogStyle);
        loadingDialog.setCancelable(true);
        loadingDialog.setCanceledOnTouchOutside(false);
        loadingDialog.setContentView(contentView, new LinearLayout.LayoutParams(-1, -1));
        Window window = loadingDialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = -1;
        lp.height = -2;
        window.setGravity(17);
        window.setAttributes(lp);
        window.setWindowAnimations(R.style.PopWindowAnimStyle);
        loadingDialog.show();
        return loadingDialog;
    }

    public static Dialog createLoadingDialog_(Context context) {
        View contentView = LayoutInflater.from(context).inflate(R.layout.layout_loading_dialog, (ViewGroup) null);
        Dialog loadingDialog = new Dialog(context, R.style.MyDialogStyle);
        loadingDialog.setCancelable(true);
        loadingDialog.setCanceledOnTouchOutside(false);
        loadingDialog.setContentView(contentView, new LinearLayout.LayoutParams(-1, -1));
        Window window = loadingDialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = -1;
        lp.height = -2;
        window.setGravity(17);
        window.setAttributes(lp);
        window.setWindowAnimations(R.style.PopWindowAnimStyle);
        return loadingDialog;
    }

    public static void closeDialog(Dialog mDialogUtils) {
        if (mDialogUtils != null && mDialogUtils.isShowing()) {
            mDialogUtils.dismiss();
        }
    }

    public static Dialog showUnifyDialog(Context context, View view) {
        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) context.getSystemService("window")).getDefaultDisplay().getMetrics(dm);
        return new MyDialog(dm.widthPixels, dm.heightPixels, context, 180, 0, view, R.style.OfflineDialogStyle);
    }

    public static Dialog showUnifyDialogs(Context context, View view) {
        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) context.getSystemService("window")).getDefaultDisplay().getMetrics(dm);
        return new MyDialog(dm.widthPixels, dm.heightPixels, context, 340, 0, view, R.style.edit_menu_style);
    }

    public static Dialog showSelectDialog(Context context, View view, int type) {
        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) context.getSystemService("window")).getDefaultDisplay().getMetrics(dm);
        return new SelectDialog(dm.widthPixels, dm.heightPixels, context, 0, 0, view, R.style.edit_menu_style, type);
    }

    public static Dialog showModeTimeDialog(Context context, View view, int widOff, int heiOff, int type) {
        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) context.getSystemService("window")).getDefaultDisplay().getMetrics(dm);
        return new SelectDialog(dm.widthPixels, dm.heightPixels, context, widOff, heiOff, view, R.style.mode_time_style, type);
    }

    public static Dialog showPatNameDialog(Context context, View view, int type) {
        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) context.getSystemService("window")).getDefaultDisplay().getMetrics(dm);
        return new PatNameDialog(dm.widthPixels, dm.heightPixels, context, 40, 0, view, R.style.patition_name_style, type);
    }

    public static Dialog showTipsDialog(Context context, View view) {
        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) context.getSystemService("window")).getDefaultDisplay().getMetrics(dm);
        return new EditTipsDialog(dm.widthPixels, dm.heightPixels, context, 60, 0, view, R.style.OfflineDialogStyle);
    }
}
